package com.wondertek.poms.core.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.core.service.IProductService;
import com.wondertek.poms.core.service.ISellingPriceTagService;
import com.wondertek.poms.dao.po.Product;
import com.wondertek.poms.dao.po.SellingPriceTag;
import com.wondertek.poms.dao.pojo.SellingPriceTagPojo;
import com.wondertek.poms.dao.repo.ISellingPriceTagDao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * <p><b>PcProductRuleServiceImpl Description:</b> 售价标签 service层实现 </p>
 *
 * @author 高正行
 * <p><b>DATE</b> 2020-10-16 15:56:15</p>
 */
@Service
@Scope("prototype")
public class SellingPriceTagServiceImpl extends ServiceImpl<SellingPriceTag, Long> implements ISellingPriceTagService {
	
	@Autowired
	private ISellingPriceTagDao sellingPriceTagDao;
	@PersistenceContext
    public EntityManager entityManager;
	@Autowired
	private IProductService productService;

	@Override
	public SellingPriceTag deleteLogic(Long id) {
		return null;
	}

	@Override
	public SellingPriceTag merge(SellingPriceTag model) {
		return null;
	}

	@Override
	public SellingPriceTag findDefaultPrice(Long prdInfoId) {
		 List<SellingPriceTag> list = sellingPriceTagDao.findDefaultPrice(prdInfoId);
		 if (list != null && list.size() > 0) {
			 return list.get(0);
		 }
		 return null;
	}

	@Override
	public Page<SellingPriceTagPojo> findAll(SellingPriceTag entity, int page, int rows, String sord, String sidx) {
		StringBuilder condition = sqlStringWhere(entity,sord,sidx);
		Object o = entityManager
				.createNativeQuery("select count(*) from ( " + condition.toString() + " ) a").getSingleResult();
		int count = Convert.toInt(o);
		Pageable pageable = getPageable(page - 1, rows, sord, sidx);

		if(count>0){
			Query query=entityManager.createNativeQuery(condition.toString(),SellingPriceTag.class);
			query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
			query.setMaxResults(pageable.getPageSize());
			List<SellingPriceTag> list=query.getResultList();
			List<SellingPriceTagPojo> pojoList=new ArrayList<>();
			for(SellingPriceTag temp:list){
				SellingPriceTagPojo pojo=new SellingPriceTagPojo();
				Product product=productService.getByProductId(temp.getProductId().toString());
				BeanUtils.copyProperties(temp,pojo);
				if(product != null){
					pojo.setProductName(product.getName());
					pojo.setDescription(product.getDescription());
				}
				pojoList.add(pojo);
			}
			PageImpl<SellingPriceTagPojo> result=new PageImpl<>(pojoList,pageable,count);
			entityManager.close();
			return result;
		}
		entityManager.close();
		return Page.empty(pageable);
	}

	@Override
	public int checkSellingPriceTagHaveDefault(String prdInfoId) {
		return sellingPriceTagDao.checkSellingPriceTagHaveDefault(Convert.toLong(prdInfoId));
	}

	public StringBuilder sqlStringWhere(SellingPriceTag entity,String sord, String sidx){
		StringBuilder condition=new StringBuilder();
		String sql="select * from SELLING_PRICE_TAG where 1=1 ";
		condition.append(sql);

		if(entity.getProductId()!=null){
			condition.append("and PRODUCT_ID= '"+entity.getProductId()+"' ");
		}
		if(StrUtil.isNotBlank(entity.getMamRecomend())){
			condition.append("and MAM_RECOMMEND= '"+entity.getMamRecomend()+"' ");
		}

		condition.append("order by "+sidx+" "+sord);
		return condition;
	}
}